<template>
  <div class="add-form">
    <el-dialog
    :title="text+pageTitle"
    :visible.sync="dialogFormVisible"
    :before-close="handleClose"
    >
      <el-form
        :rules="ruleInline"
        ref="dataForm"
        :model="formBase"
        label-position="left"
        label-width="120px"
        style="width: 400px; margin-left:120px;"
      >

        <el-form-item :label="$t('table.username')" prop="username">
          <el-input v-model="formBase.username"></el-input>
        </el-form-item>
        <el-form-item :label="$t('table.email')" prop="email">
          <el-input v-model="formBase.email"></el-input>
        </el-form-item>
        <el-form-item
          :label="$t('table.paddword')"
          prop="password"
          v-if="text === '编辑'? false : true"
        >
          <el-input v-model="formBase.password"></el-input>
        </el-form-item>
        <!-- 性别 -->
        <el-form-item prop="sex" label="性别"   v-if="text === '编辑'? false : true">
          <el-input v-model="formBase.sex"></el-input>
        </el-form-item>
        <!-- 角色 -->
        <el-form-item :label="$t('table.role')" prop="role">
          <el-input v-model="formBase.role"></el-input>
        </el-form-item>
        <!-- 权限组 -->
        <el-form-item :label="$t('table.permissionUser')" prop="permission_group_id">
          <el-select class="filter-item" v-model="formBase.permission_group_id">
            <el-option
              v-for="item in PermissionGroupsList"
              :value="item.id"
              :key="item.key"
              :label="item.title"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('table.phone')" prop="phone">
          <el-input v-model="formBase.phone"></el-input>
        </el-form-item>

        <!-- 头像上传下一个版本再做 -->
        <!-- <el-form-item :label="$t('table.avatar')" prop="avatar">
            <el-upload
              class="upload-demo"
              :action="importFileUrl"
              :on-change="handleChange"
              :file-list="fileList" accept="image/jpeg,image/gif,image/png,image/bmp"
              :before-upload="beforeAvatarUpload">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
        </el-form-item>-->
        <el-form-item :label="$t('table.introduction')">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="Please input"
            v-model="formBase.introduction"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">{{$t('table.cancel')}}</el-button>
        <el-button type="primary" @click="createData">{{$t('table.confirm')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { update as updateUser, add as addUser } from '@/api/base/users'
export default {
  name: 'usersAdd',
  props: [
    // 标题
    'text',
    // 标题文本
    'pageTitle',
    // 循环的选项
    'PermissionGroupsList',
    // 要提交的表单
    'formBase',
    // 规则
    'ruleInline',
    // 密码输入框的显示隐藏
    'deleteShow'
    // // 控制显示隐藏
    // 'dialogFormVisible'
  ],
  data () {
    return {
      dialogFormVisible: false
      // fileList: [],
      // importFileUrl: 'https://jsonplaceholder.typicode.com/posts/',
    }
  },
  computed: {},
  methods: {
    // 弹层显示
    dialogFormV () {
      this.dialogFormVisible = true
    },
    // 弹层隐藏
    dialogFormH () {
      this.dialogFormVisible = false
    },
    // 退出
    handleClose () {
      this.$refs.dataForm.resetFields()
      this.$emit('handleCloseModal')
    },

    // 表单提交
    // createData () {
    //   this.$refs.dataForm.validate(valid => {
    //     if (valid) {
    //       this.$emit('handleCloseModal')
    //       const data = {
    //         ...this.formBase
    //       }
    //       if (this.formBase.id) {
    //         update(data).then(() => {
    //           this.$emit('newDataes', this.formBase)
    //         })
    //       } else {
    //         add(data).then(() => {
    //           this.$emit('newDataes', this.formBase)
    //         })
    //       }
    //     } else {
    //       this.$Message.error('*号为必填项!')
    //     }
    //   })
    // }
    async createData () {
      // 编辑的提交按钮
      await this.$refs.dataForm.validate()
      if (this.text === '编辑') {
        await updateUser({
          avatar: this.formBase.avatar,
          email: this.formBase.email,
          id: this.formBase.id,
          introduction: this.formBase.introduction,
          permission_group_id: this.formBase.permission_group_id,
          phone: this.formBase.phone,
          role: this.formBase.role,
          username: this.formBase.username
        })
        this.$message.success('编辑成功')
        this.$emit('newDataes', this.formBase)
      } else {
        await addUser(this.formBase)
        this.$message.success('新增成功')
        this.$emit('newDataes', this.formBase)
      }
    }
  },
  // 挂载结束

  mounted: function () {},
  // 创建完毕状态
  created () {},
  // 组件更新
  updated: function () {}
}
</script>
<style>
.el-form--label-left .el-form-item__label {
  text-align: right;
}
.el-form-item--medium {
  margin-bottom: 22px;
}
.el-dialog__footer {
  text-align: center
}
</style>
